package cn.gson.crm.controller.dept;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.entity.Dept;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeptDeleteServlet
 */
@WebServlet("/dept/delete")
public class DeptDeleteServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	DeptDao dd = new DeptDao();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] ids = request.getParameterValues("id");
		List<Dept> dept = dd.getAllDept();
		request.setAttribute("dept", dept);
		int count = 0;
		for (String id : ids) {
			if(dd.searchUpDeptId(Integer.parseInt(id)) > 0){
				request.setAttribute("dept", dept);
				errorForward("请先删除该部门下的子部门!", "dept/dept_manager.jsp");
				return;
			}
			dd.deleteDept(Integer.parseInt(id),0,0,"0");
			count++;
		}
		if(count == ids.length){
			redirect("dept/form");
			return;
		}
		request.setAttribute("dept", dept);
		errorForward("删除失败!", "dept/dept_manager.jsp");
		return;
	}

}
